<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */
class ome_ctl_admin_ivantext extends desktop_controller{
    
    var $workground = 'ome_ctl_admin_ivantext';
    var $pagelimit = 10;
    var $axsupplier_model ;
    public function __construct($app)
    {
        parent::__construct($app);
        $this->member_model = $this->app->model('axsupplier');
        header("cache-control: no-store, no-cache, must-revalidate");
    }
   function index(){
   	
        /*$custom_actions[] = array('label'=>app::get('ome')->_('新增'),'href'=>'index.php?app=ome&ctl=admin_ivantext&act=add_page','target'=>'dialog::{title:\''.app::get('ome')->_('新增').'\',width:560,height:300}');*/
        
        $custom_actions= array(
            'new' => array(
                'label'=>app::get('ome')->_('新增'),'href'=>'index.php?app=ome&ctl=admin_ivantext&act=add_page','target'=>'dialog::{title:\''.app::get('ome')->_('新增').'\',width:800,height:550}',
            ),
            'approve' => array(
                'label' => '审核',
                'submit' => 'index.php?app=ome&ctl=admin_ivantext&act=doapprove',
            ),
            'unapprove' => array(
                'label' => '弃审',
                'submit' => 'index.php?app=ome&ctl=admin_ivantext&act=dounapprove',
            ),
            'create' => array(
                'label' => '生成明细',
                'submit' => 'index.php?app=wms&ctl=admin_ivantext&act=doCreate',
            ),
            'cancel' => array(
                'label' => '取消生成',
                'submit' => 'index.php?app=wms&ctl=admin_ivantext&act=doCancel',
            ),
            'sync' => array(
                'label' => '同步',
                'submit' => 'index.php?app=wms&ctl=admin_ivantext&act=doConfirm',
            ),
        );

        $actions_base['title'] = app::get('ome')->_('Ivan测试文档');
        $actions_base['actions'] = $custom_actions;
        $actions_base['allow_detail_popup'] = true;
        $actions_base['use_buildin_set_tag'] = false;
        $actions_base['use_buildin_export'] = false;
        $actions_base['use_buildin_filter'] = false;

        $actions_base['use_view_tab'] = false;

        $this->finder('ome_mdl_ivantext',$actions_base);
    }

    function add_page()
    {

        $sql = "SELECT shop_id,shop_bn,name from sdb_ome_shop where disabled='false'";
        $shop_datas = kernel::database()->select($sql);
        $this->pagedata['shopdata'] = $shop_datas;
        //$this->pagedata['pam_account']['operator']=kernel::single('desktop_user')->get_name();
        $this->display('admin/ivantext/new.html');
    }

    function add(){
        //每次都要执行这行代码
        $this->begin('index.php?app=ome&ctl=admin_ivantext&act=index');
        //$ivantext_model = $this->app->model('ivantext');

        $start = strtotime($_POST['pam_account']['begin_date']);
        $end = strtotime($_POST['pam_account']['end_date']);
        if ($start>$end) {
            $this->end(false,"时间错误，请重新选择时间");
        }
            /*$arr_order['pam_account']['shop_id'] = $_POST['pam_account']['shop_id'];
            $arr_order['pam_account']['code'] = $_POST['pam_account']['code'];
            $arr_order['pam_account']['name'] = $_POST['pam_account']['name'];
            $arr_order['pam_account']['begin_date'] = $start;
            $arr_order['pam_account']['end_date'] = $end;
            $arr_order['pam_account']['create_date'] = time();
            $arr_order['pam_account']['operator'] = $_POST['operator'];
            $arr_order['pam_account']['status'] = $_POST['pam_account']['status'];
*/
        //判断id是否存在，不存在则新增数据，存在则修改数据
        $data=$_POST;
        $id=$_POST['id'];
        $ivanMd = app::get('ome')->model('ivantext');
        $ivanMdl = app::get('ome')->model('ivantextdetails');
        $delivery_ID = $_POST['delivery_ID'];
        $memo = $_POST['memo'];
        $operator = $_POST['operator'];
        //$res = $ivantext_model->insert($arr_order['pam_account']);
        if(empty($id))
        {
            $arr_order['pam_account']['shop_id'] = $_POST['pam_account']['shop_id'];
            $arr_order['pam_account']['code'] = $_POST['pam_account']['code'];
            $arr_order['pam_account']['name'] = $_POST['pam_account']['name'];
            $arr_order['pam_account']['begin_date'] = $start;
            $arr_order['pam_account']['end_date'] = $end;
            $arr_order['pam_account']['create_date'] = time();
            $arr_order['pam_account']['operator'] = $_POST['operator'];
            $arr_order['pam_account']['status'] = $_POST['pam_account']['status'];
            $res = $ivanMd->insert($arr_order['pam_account']);
        }else
        {
            $res=$id;
            $data['shop_id'] =$_POST['pam_account']['shop_id'];
            $data['begin_date'] =$start;
            $data['end_date'] =$end;
            $data['operator']=$_POST['operator'];
            $data['id'] = $id;
            app::get('ome')->model('ivantext')->save($data);
            kernel::database()->exec("delete from sdb_ome_ivantextdetails  where  ivantext_id in(" . $id . ");");
        } 
        foreach ($delivery_ID as $key => $value)
        {
            $arr_line='';
            $arr_line['spl']['ivantext_id']=$res;
            $arr_line['spl']['bn']=$value['material_bn'];
            $arr_line['spl']['name']=$value['material_name'];
            $arr_line['spl']['nums']=$value['number'];
            $arr_line['spl']['price']=$value['price'];
            $arr_line['spl']['amount']=$value['amount'];
            $ivanMdl->insert($arr_line['spl']);
            unset($arr_line);  
            
        }
        $this->end(true, '已完成');

        //保存明细 

        /*$ivanMdl = app::get('ome')->model('ivantextdetails');
         
        $res = $ivantext_model->insert($arr_order['pam_account']);
        $delivery_ID = $_POST['delivery_ID'];
        foreach ($delivery_ID as $key => $value)
        {
            $arr_line='';
            $arr_line['spl']['ivantext_id']=$res;
            $arr_line['spl']['bn']=$value['material_bn'];
            $arr_line['spl']['name']=$value['material_name'];
            $arr_line['spl']['nums']=$value['number'];
            $arr_line['spl']['price']=$value['price'];
            $arr_line['spl']['amount']=$value['amount'];
            $ivanMdl->insert($arr_line['spl']);
            unset($arr_line);   
        }
        if($_POST)
        {
            if($_POST['pam_account']['id'] != '')
            {

                $db = kernel::database();
                $db->beginTransaction();
                unset($arr_order['pam_account']['create_date']);
                if( $ivantext_model->update($arr_order['pam_account'], array('id' => $_POST['pam_account']['id'])))    
                {

                    $db->commit();
                }else{
                    $msg = '保存失败，请联系客服!';
                    return false;
                }
                $this->end(true, app::get('ome')->_('修改成功！'));
            } 
            else 
            {
                
                $db = kernel::database();
                $db->beginTransaction();
                if( $ivantext_model->save($arr_order['pam_account']) )

                {
                    $db->commit();
                }else{
                    $msg = '保存失败，请联系客服!';
                    return false;
                }
                $this->end(true, app::get('ome')->_('添加成功！'));
            }
        }*/
        
    }
    function lmedit(){
        /*$ivanMdl = $this->app->model('ivantext');
        $ivantext_datas = $ivanMdl->getList('*', array('id'=>$_GET['id']), 0, -1);

        
        $sql = "SELECT shop_id,shop_bn,name from sdb_ome_shop where disabled='false'";
        $shop_datas = kernel::database()->select($sql);
        foreach ($shop_datas as $v) {
            $shop_data[$v['shop_id']] ='【'.$v['shop_bn'].'】'. $v['name'];
        }
        $this->pagedata['shop_data'] = $shop_data;
        $this->pagedata['pam_account'] = $ivantext_datas[0];
        $this->pagedata['pam_account']['begin_date'] =date('Y-m-d',$ivantext_datas[0]['begin_date']) ;
        $this->pagedata['pam_account']['end_date'] =date('Y-m-d',$ivantext_datas[0]['end_date']) ;
        $this->singlepage('admin/ivantext/new_edit.html');*/
        if(isset($_GET['id'])){
            $id = $_GET['id'];
            $ome_mdl_ivantext_obj = kernel::single('ome_mdl_ivantext');
            $ivantext = $ome_mdl_ivantext_obj->getList('*', array('id'=>$id), 0, 1);

        }

        $sql = "SELECT shop_id,shop_bn,name from sdb_ome_shop where disabled='false'";
        $shop_datas = kernel::database()->select($sql);
        $this->pagedata['shopdata'] = $shop_datas;
        $this->pagedata['pam_account'] = $ivantext[0];
        $this->page('admin/ivantext/new_edit.html');

    }

    //获取后台数据
    function getEditProducts($po_id){
        if ($po_id == ''){
            $po_id = $_POST['p[0]'];
        }
        $sql="SELECT * from sdb_ome_ivantextdetails  where ivantext_id=".$po_id;

        $db=kernel::database();
        $rows=$db->select($sql);  
        $i=0;
        foreach ($rows as $key => $value)
        {
            $items[$key]=$value;
            $items[$key]['product_id']=$i;    
            $items[$key]['material_bn']=$value['bn'];    
            $items[$key]['material_name']=$value['name'];
            $items[$key]['number']=$value['nums'];
            $i=$i+1;
        }
        echo json_encode($items);
    }

    //获取物料明细
    function findmaterialbasic($supplier_id=null)
    {       
        $params = array(
                'title'=>'基础物料',
                'use_buildin_new_dialog' => false,
                'use_buildin_set_tag'=>false,
                'use_buildin_recycle'=>false,
                'use_buildin_export'=>false,
                'use_buildin_import'=>false,
                'use_buildin_filter'=>true,
                'base_filter' => $base_filter,
        );
        $this->finder('material_mdl_basic_material', $params);
    }
    
    
    function getmaterialbasic()
    {
        $basicMaterialObj = app::get('material')->model('basic_material');
        $omeExtOrdLib = kernel::single('ome_extint_order');
        $pro_id = $_POST['product_id'];

        $basicMaterialData= $basicMaterialObj->getList('*',array('bm_id' => $pro_id ));
        if (!empty($basicMaterialData))
        {
            foreach ($basicMaterialData as $k => $item)
            {
                $itm['product_id']=$item['bm_id'];
                $itm['material_bn']=$item['material_bn'];
                $itm['material_name']=$item['material_name'];
                $rows[]= $itm;

            }
        }
        echo "window.autocompleter_json=".json_encode($rows);
    }

    //审核
    function doapprove()
    {
        $this->updatestatus(1);
    }
    //弃审
    function dounapprove()
    {
        $this->updatestatus(0);
    }
    function updatestatus($is_approve)
    {
        $max_input_vars = ini_get('max_input_vars');
        ini_set('memory_limit','512M');
        set_time_limit(600);
        $this->begin("index.php?app=ome&ctl=admin_ivantext&act=index");
        $this->_request    = kernel::single('base_component_request');
        $order_info        = $this->_request->get_post();
        $db=kernel::database();
        $spdocMdl = $this->app->model('ivantext');
        if ($order_info['isSelectedAll']=='_ALL_') 
        {
            foreach ($orders as $key_o => $order) 
            {
                $value = $order['id']; 
                $spdocdata=$spdocMdl->dump($value,"*");
                if($is_approve==1)
                {
                    if($spdocdata['status']!='unconfirmed'){
                        $this->end(false, $spdocdata['sp_bn'].'状态异常，审核失败！');
                    }

                    $db->exec("update sdb_ome_ivantext set status='confirmed',last_modify='".time()."' where  id in(" . $value . ");");   
                }else if($is_approve==0)
                {
                    if($spdocdata['status']!='confirmed'){
                        $this->end(false, $spdocdata['sp_bn'].'状态异常，弃审失败！');
                    }
                    //$db->exec("delete from sdb_wms_spadjustmentdoc_record  where  spadjustmentdoc_id in(" .$value . ");");
                    $db->exec("update sdb_ome_ivantext set status='unconfirmed',last_modify='".time()."' where  id in(" . $value . ");");   
                }
                $this->end(true, '操作成功');
            }
        }else
        {
            $input = file_get_contents("php://input");
            $input_array = explode('&', $input);
            $number = count($input_array);
            if ($number > $max_input_vars) {
                $msg =  "多选不能超过系统表单最大提交数【".$max_input_vars."】条";
                $this->end(false, $msg);
            }
            foreach ($order_info['id'] as $key_o => $value)
            {   
                $spdocdata=$spdocMdl->dump($value,"*");
                if($is_approve==1)
                {
                    if($spdocdata['status']!='unconfirmed'){
                        $this->end(false, $spdocdata['sp_bn'].'状态异常，审核失败！');
                    }
                    $db->exec("update sdb_ome_ivantext set status='confirmed',last_modify='".time()."' where  id in(" . $value . ");");   
                }else if($is_approve==0)
                {
                    if($spdocdata['status']!='confirmed'){
                        $this->end(false, $spdocdata['sp_bn'].'状态异常，弃审失败！');
                    }
                    //$db->exec("delete from sdb_wms_spadjustmentdoc_record  where  spadjustmentdoc_id in(" .$value . ");");
                    $db->exec("update sdb_ome_ivantext set status='unconfirmed',last_modify='".time()."' where  id in(" . $value . ");");   
                }
                $this->end(true, '操作成功');
            }
        }
        $this->end(true, '已完成');
    }

    //理文测试
    
}
